/*
 * @(#)CollectionManager.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.model.media;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.cdavies.itunes.Track;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.netexplode.jtunes.CollectionType;

/**
 * <code>CollectionManager</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.2 $
 * @since 0.1
 */
public class CollectionManager implements ApplicationContextAware {

	private ApplicationContext applicationContext;

	private CollectionType collectionType = CollectionType.Local;

	private List<MediaCollection> localCollections = new ArrayList<MediaCollection>();

	private List<MediaCollection> sharedCollections = new ArrayList<MediaCollection>();

	private int localPosition;

	private int sharedPosition;

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	public MediaCollection newLocalCollection(String name, String source) throws Exception {
		LocalCollection collection = (LocalCollection) applicationContext.getBean(LocalCollection.class.getName());
		collection.setName(name);
		collection.setDevice(new File(source));
		collection.init();

		return collection;
	}

	public MediaCollection newSharedCollection() throws IOException {
		SharedCollection collection = (SharedCollection) applicationContext.getBean(SharedCollection.class.getName());
		collection.init();

		return collection;
	}

	public CollectionType getCollectionType() {
		return this.collectionType;
	}

	public void setCollectionType(CollectionType collectionType) {
		this.collectionType = collectionType;
	}

	public List<MediaCollection> getLocalCollections() {
		return localCollections;
	}

	public void setLocalCollections(List<MediaCollection> collections) {
		this.localCollections = collections;
	}

	public int getLocalPosition() {
		return this.localPosition;
	}

	public void setLocalPosition(int localPosition) {
		this.localPosition = localPosition;
	}

	public synchronized List<MediaCollection> getSharedCollections() {
		return this.sharedCollections;
	}

	public synchronized void setSharedCollections(List<MediaCollection> sharedCollections) {
		this.sharedCollections = sharedCollections;
	}

	public int getSharedPosition() {
		return this.sharedPosition;
	}

	public void setSharedPosition(int sharedPosition) {
		this.sharedPosition = sharedPosition;
	}

	private int getSongPosition() {
		return this.getCurrrentCollection().getPosition();
	}

	public void movePrevious() {
		int songIndex = this.getSongPosition();
		if (songIndex == 0) {
			songIndex = this.getCurrrentCollection().size() - 1;
		}
		else {
			songIndex--;
		}
		this.getCurrrentCollection().setPosition(songIndex);
	}

	public void moveNext() {
		int songIndex = this.getSongPosition();
		if (songIndex == (this.getCurrrentCollection().size() - 1)) {
			songIndex = 0;
		}
		else {
			songIndex++;
		}
		this.getCurrrentCollection().setPosition(songIndex);
	}

	public MediaCollection getCurrrentCollection() {

		if (collectionType == CollectionType.Local) {
			return (MediaCollection) this.getLocalCollections().get(this.getLocalPosition());
		}
		else if (collectionType == CollectionType.Shared) {
			return (MediaCollection) this.getSharedCollections().get(this.getLocalPosition());
		}

		return null;
	}

	public MediaFile getCurrentMediaFile() {

		if (collectionType == CollectionType.Local) {
			return this.getCurrrentCollection().getMediaFile(this.getSongPosition());
		}
		else if (collectionType == CollectionType.Shared) {
			final SharedCollection sharedCol = (SharedCollection) this.getSharedCollections().get(
					this.getSharedPosition());
			Track track = sharedCol.getTracks()[this.getSongPosition()];
			return new ItunesFile(track);
		}

		return null;
	}

	public Track getCurrentItunesTrack() {
		return (Track) this.getSharedCollections().get(this.getSharedPosition());
	}
}